/**
 * jQuery.jSelectCity Version 1.0
 * jQuery 下拉列表选择城市插件
 * 作者:LS
 */
/*
 * *****************   Example   ***********************
 <script type="text/javascript">
 $("body").ready(function(){
 	$("#city").selectcity();
 })
 </script>
 
 <body>
 <input type="text" id="city" value="浙江-宁波-鄞州" />
 </body>
 * ****************  End Example  **********************
 */
(function($) {
	$.jSelectCity = {
		/**
		* 选项设置
		*/
		settings : {
	        css			: "city",
	        borderCss	: "city",
	        split		: "-",
	        province	: "北京",
	        disabled	: false
		},
		
	    /**
	     * 初始化对向
	     * @param {Object} el 用于存放地址结果的文本框 jQuery DOM
	     */
	    init : function(els){
	        els.each(function(){
	            var el = $(this);
	            
	            /* 取得旧的地址 */
	            var elValue = el.val();
	            elData = elValue.split($.jSelectCity.settings.split);
	            
	            var nProvince  = elData[0]!=""?elData[0]:$.jSelectCity.settings.province;
	            var nCity = elData[1];
	            var nTown = elData[2];
	            
	            /* 隐藏给出的对向 */
	            el.hide();
	            
	            /* 先算出当前共有多少个jSelectCity */
	            var currentIdx = $("jSelectCityBorder").length + 1;
	                        
	            /* 加入控件到文本框的位置 */
	            var spanCity = $("<span>",{id:"spanCity" + currentIdx, "class":"jSelectCityBorder", "disabled":$.jSelectCity.settings.disabled});
	
	            el.after(spanCity);
	
	            /* 创建省 */
	            var selProvince = $("<select>",{id:"selProvince" + currentIdx, "class":$.jSelectCity.settings.css, "disabled":$.jSelectCity.settings.disabled});
	            
	            /* 加入选项 */
	            var inProvince = 0;
	            $.each( GP , function(index,data){
	            	var option = $("<option>").val(data).html(data);
	                if (data == nProvince) {
	                    option.attr("selected",true);
	                    inProvince = index;
	                }
	            	selProvince.append(option);
	            	option = null;
	        	});
	            var divProvince = $("<div>",{id:"divProvince" + currentIdx,"class":$.jSelectCity.settings.borderCss});
	            selProvince.appendTo(divProvince);
	            spanCity.append(divProvince);
	            
	            /* 创建一级城市 */
	            var selCity = $("<select>",{id:"selCity" + currentIdx, "class":$.jSelectCity.settings.css, "disabled":$.jSelectCity.settings.disabled});
	            
	            /* 加入选项 */
	            var inCity = 0;
	            $.each( GT[inProvince] , function(index,data){
	            	var option = $("<option>").val(data).html(data);
	                if (data == nCity) {
	                    option.attr("selected",true);
	                    inCity = index;
	                }
	            	selCity.append(option);
	            	option = null;
	        	});
	            
	            /* 加入控件到文本框的位置 */
	            var divCity = $("<div>",{id:"divCity" + currentIdx,"class":$.jSelectCity.settings.borderCss});
	            selCity.appendTo(divCity);
	            divProvince.after(divCity);
	            
	            selProvince.children("option[value='" + nProvince + "']").attr("selected",true);
	            /* 创建二级城市 */
	            var selTown = $("<select>",{id:"selTown" + currentIdx, "class":$.jSelectCity.settings.css, "disabled":$.jSelectCity.settings.disabled});
	            
	            /* 加入选项 */
	            $.each( GC[inProvince][inCity] , function(index,data){
	            	var option = $("<option>").val(data).html(data);
	                if (data == nTown) {
	                    option.attr("selected",true);
	                }
	            	selTown.append(option);
	            	option = null;
	        	});
	            
	            /* 加入控件到文本框的位置 */
	            var divTown = $("<div>",{id:"divTown" + currentIdx,"class":$.jSelectCity.settings.borderCss});
	            selTown.appendTo(divTown);
	            divCity.after(divTown);
	
	            /* 返回当前选择的地址 */
	            var getCity = function(){
	                var province = selProvince.val();
	                var city = selCity.val();
	                var town = selTown.val();
	                el.val(province + "-" + city + "-" + town);
	            }
	            
	            /**
	             * 给几个下拉列表加入更改后的事件
	             */
	            selTown.change(function(){
	                return getCity();
	            });
	            var index2 = "";
	            selCity.change(function(){
	            	selTown.empty();
	            	index1 = selProvince.get(0).selectedIndex;
	            	index2 = this.selectedIndex;
	        		$.each( GC[index1][index2] , function(index,data){
	        			selTown.append("<option value='"+data+"'>"+data+"</option>");
	        		});
			        /* 更新文本框中的地址 */
	                return getCity();
	            });
	            var index1 = "";
	            selProvince.change(function(){
	            	//清空其它2个下拉框
	            	selCity.empty();
	            	selTown.empty();
	            	index1 = this.selectedIndex;
	        		
	    			$.each( GT[index1] , function(index,data){
	    				selCity.append("<option value='"+data+"'>"+data+"</option>");
	    			});
	    			$.each( GC[index1][0] , function(index,data){
	    				selTown.append("<option value='"+data+"'>"+data+"</option>");
	    			});
	        		return getCity();
	            });
	            el.parents("form").submit(function(){
	            	if(el.val() == '')getCity();
	            });
	        })
	    }
	}

	$.fn.selectcity = function(settings) {
		$.extend($.jSelectCity.settings, settings);
		$.jSelectCity.init($(this));
	}
})(jQuery)